import django_filters

from .models import Prize, PrizeAuthor


class PrizeFilter(django_filters.rest_framework.FilterSet):
    title = django_filters.CharFilter(lookup_expr="icontains")
    teacher_faculty = django_filters.CharFilter(method='filter_teacher_faculty')
    student_faculty = django_filters.CharFilter(method='filter_student_faculty')
    author_name = django_filters.CharFilter(method='filter_author_name')

    @staticmethod
    def filter_teacher_faculty(queryset, name, value):
        # 找出该角色名对应的id
        prize_ids = PrizeAuthor.objects.filter(faculty_code=value, author_type='本校老师').values_list('prize_id', flat=True)
        return queryset.filter(id__in=prize_ids)

    @staticmethod
    def filter_student_faculty(queryset, name, value):
        # 找出该角色名对应的id
        prize_ids = PrizeAuthor.objects.filter(faculty_code=value, author_type='本校学生').values_list('prize_id',
                                                                                                   flat=True)
        return queryset.filter(id__in=prize_ids)

    @staticmethod
    def filter_author_name(queryset, name, value):
        # 找出该角色名对应的id
        prize_ids = PrizeAuthor.objects.filter(name__icontains=value).values_list('prize_id', flat=True)
        return queryset.filter(id__in=prize_ids)

    class Meta:
        model = Prize
        fields = ['title', 'first_check_status', 'second_check_status']
